package com.example.nophase.custom;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.example.nophase.R;

public class CutInputDialog extends Dialog {
    private Context context;
    private String title = "";
    private String hint = "";
    private String confirmBtnText = "";
    private String cancelBtnText = "";
    private String[] namelist;
    private CutInputDialog.ClickListenerInterface clickListenerInterface;
    private EditText inputMsg;

    public interface ClickListenerInterface {
        void doConfirm();

        void doCancel();
    }

    public CutInputDialog(Context context, String title ) {
        super(context, R.style.customDialog);
        this.context = context;
        this.title = title;

    }

    public CutInputDialog(Context context, String title, String confirmBtnText, String cancelBtnText,String[] namelist) {
        this(context, title);
        this.confirmBtnText = confirmBtnText;
        this.cancelBtnText = cancelBtnText;
        this.namelist = namelist;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        init();
    }

    public String getHint() {
        return hint;
    }

    public void setHint(String hint) {
        this.hint = hint;
        inputMsg.setHint(hint);
    }

    public String getInputMsg(){
        return inputMsg.getText().toString();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.cut_input_dialog_layout, null);
        setContentView(view);

        TextView tvTitle = view.findViewById(R.id.my_dialog_title);
        inputMsg = view.findViewById(R.id.my_dialog_input);
        Button btnConfirm = view.findViewById(R.id.btn_main_activity_comfirm);
        Button btnCancel = view.findViewById(R.id.btn_main_activity_cancel);
        Button cutBtn = view.findViewById(R.id.cut_btn);
        if(namelist.length == 0){
            cutBtn.setVisibility(View.GONE);
        }
        cutBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCheckedDialog(namelist);
            }
        });
        tvTitle.setText(title);

        if (!confirmBtnText.isEmpty() && !cancelBtnText.isEmpty()) {
            btnConfirm.setText(confirmBtnText);
            btnCancel.setText(cancelBtnText);
        }

        btnConfirm.setOnClickListener(new CutInputDialog.CustomDialogClickListener());
        btnCancel.setOnClickListener(new CutInputDialog.CustomDialogClickListener());

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    public void setClicklistener(CutInputDialog.ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private void showCheckedDialog(String[] namelist) {
        String[] name = new String[1];
        name[0] = namelist[0];
        new AlertDialog.Builder(context)
                .setTitle("请选择项目")//提示框标题
                .setIcon(R.drawable.apply_for_from)
                .setSingleChoiceItems(
                        namelist, 0,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                name[0] = namelist[which];
                            }
                        }
                )
                .setPositiveButton("确定",//提示框的两个按钮
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                inputMsg.setText(name[0]);
                            }
                        })
                .setNegativeButton("取消", null).create().show();
    }

    private class CustomDialogClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int id = v.getId();
            switch (id) {
                case R.id.btn_main_activity_comfirm:
                    clickListenerInterface.doConfirm();
                    break;
                case R.id.btn_main_activity_cancel:
                    clickListenerInterface.doCancel();
                    break;
            }
        }
    }

}
